<template>
  <view class="search-box">
    <my-search v-on:click="gotoSearch"></my-search>
  </view>
  <!-- 轮播图 快捷键usw-->
  <view>    
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" v-bind:circular="true">
      <swiper-item v-for="(item, index) in swiperList" v-bind:key="index">
        <navigator class="swiper-item" v-bind:url="'subpkg/good_detail/good_detail?goods_id='+ item.goods_id">
          <image v-bind:src="item.image_src" mode=""></image>
        </navigator>
      </swiper-item>
    </swiper>
  </view>
  
  <!-- 分类导航 -->
  <view class="nav-list">
    <view class="nav-item" v-for="(item, index) in navList" v-bind:key="index" v-on:click="navClickHandler(item)">
      <image v-bind:src="item.image_src" mode="" class="nav-img"></image>
    </view>
  </view>
  
  <!-- 楼层 -->
  <view class="floor-list">
    <view class="floor-item" v-for="(item, index) in floorList" v-bind:key="index"  >
      <image v-bind:src="item.floor_title.image_src" mode="" class="floor-title"></image>
      <!-- 楼层图片区域 -->
      <view class="floor-img-box">
        <navigator class="left-img-box" v-bind:url="item.product_list[0].url">
          <image v-bind:src="item.product_list[0].image_src" mode="widthFix" v-bind:style="{width: item.product_list[0].image_width + 'rpx'}"></image>
        </navigator>
        <view class="right-img-box">
          <navigator class="right-img-item" v-for="(jtem, j) in item.product_list" v-bind:key="j" v-bind:url="jtem.url">
            <image v-bind:src="jtem.image_src" mode="widthFix" v-bind:style="{width: jtem.image_width + 'rpx'}" v-if="j > 0"></image>
          </navigator>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  export default {   
    mixins: [badgeMix],
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: []
      };
    },
    onLoad(){
      this.getSwiperList(),
      this.getNavList(),
      this.getFloorList()
    },
    methods: {
      async getSwiperList() {
        const res = await uni.$http.get('/api/public/v1/home/swiperdata');
        console.log('result1: ',res)   
        if(res.data.meta.status != 200){
          return uni.showToast({
            title: '数据请求失败...',
            duration: 1500,
            icon: 'none'
          })
        }
        this.swiperList = res.data.message;
      },
      async getNavList(){
        const res = await uni.$http.get('/api/public/v1/home/catitems');
        console.log('result2: ',res);
        if(res.data.meta.status != 200){
          return uni.$showMessage()
        }
        this.navList = res.data.message;
      },
      navClickHandler(item){
        if(item.name == '分类'){
          uni.switchTab({
            url: "/pages/cate/cate"
          });
        }
      },
      async getFloorList(){
        const res = await uni.$http.get('/api/public/v1/home/floordata');
        console.log('result3: ',res);
        if(res.data.meta.status != 200){
          return uni.$showMessage();
        }
        //对数据作处理
        res.data.message.forEach(floorItem => {
          floorItem.product_list.forEach(item => {
            item.url = '/subpkg/good_list/good_list?' + item.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.data.message;
      },
      gotoSearch(){
        uni.navigateTo({
          url: "/subpkg/search/search"
        })
      }
    }
  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;
    .swiper-item, image {
      width: 100%;
      height: 100%;
    }
  }
  
  .nav-list{
    display: flex;
    justify-content: space-around;
    margin: 15px, 0;
    .nav-img{
      width: 128rpx;
      height: 140rpx;
    }
  }
  
  .floor-title{
    width: 100%;
    height: 60rpx;
  }
  
  .right-img-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }
  
  .floor-img-box{
    display: flex;
    padding-left: 10rpx;
  }
  .search-box{
    position: sticky; //设置定位效果为“吸顶”
    top: 0;
    z-index: 999; //提高层级, 防止被轮播图覆盖
  }
</style>
